package zhongbo.java.air.monitor.Service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.dto.PageQueryDto;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.monitor.Service.AirAlertLogService;
import zhongbo.java.air.monitor.mapper.AirAlertLogMapper;
import zhongbo.java.air.monitor.model.AirAlertLog;

@Service
@Slf4j
public class AirAlertLogServiceImpl extends ServiceImpl<AirAlertLogMapper, AirAlertLog> implements AirAlertLogService {


    @Autowired
    private AirAlertLogMapper airAlertLogMapper;

    @Override
    public PageVo<AirAlertLog> pageQuery(Integer page, Integer pageSize
            , String icao, String startDate, String endDate
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirAlertLog> queryWrapper = new QueryWrapper<>();
        Page<AirAlertLog> airAlertLogPage = new Page<>(page,pageSize);
        if (oderByField == null || oderByField.isEmpty()){
            queryWrapper.orderByAsc("updated_time");
        }else {
            if (orderByAsc){
                queryWrapper.orderByAsc(oderByField);
            }else {
                queryWrapper.orderByDesc(oderByField);
            }
        }

        //指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }

        Page<AirAlertLog> airAlertLogPage1 = airAlertLogMapper.selectPage(airAlertLogPage, queryWrapper);
        return new PageVo<>(airAlertLogPage1);
    }
}
